package micro

import (
	"flare-admin/db/sqlc/micro"
	"flare-admin/db/sqlc/reversesh"
	"flare-admin/http/request"
	"strconv"
	"time"
)

func PatternDao2DTOs(patterns []micro.SelectAllPatternRow) []PatternDTO {
	var patternDTOs []PatternDTO
	for _, pattern := range patterns {
		patternDTO := PatternDTO{
			Name:      pattern.Name,
			GraphData: pattern.GraphData,
			Desc:      pattern.Desc,
		}
		patternDTOs = append(patternDTOs, patternDTO)
	}
	return patternDTOs
}

func ReverseShConfigDao2DTOs(configs []reversesh.SelectAllConfigRow) []ReverseShConfigDTO {
	var configDTOs []ReverseShConfigDTO
	for _, config := range configs {
		configDTO := ReverseShConfigDTO{
			NodeIP:        config.NodeIp,
			PluginEnabled: config.PluginEnabled,
		}
		configDTOs = append(configDTOs, configDTO)
	}
	return configDTOs
}

func PatternMatchResultRequest2DTO(req *request.PatternMatchResultRequest) (*PatternMatchResultDTO, error) {
	a, err := strconv.ParseInt(req.AnalyseTime, 10, 64)
	if err != nil {
		a = 0
	}
	b, err := strconv.ParseInt(req.InvokeTime, 10, 64)
	if err != nil {
		b = 0
	}

	analyseTime := time.Unix(0, a)
	invokeTime := time.Unix(0, b)
	return &PatternMatchResultDTO{
		ID:             req.ID,
		PatternID:      req.PatternID,
		CybertwinID:    req.CybertwinID,
		CybertwinLabel: req.CybertwinLabel,
		AnalyseTime:    analyseTime,
		InvokeTime:     invokeTime,
	}, err
}

func AddPatternRequest2DTO(req *request.AddPatternRequest) *PatternDTO {
	return &PatternDTO{
		Name:      req.Name,
		Desc:      req.Desc,
		GraphData: req.GraphData,
	}
}
